package gdao

import (
	"RedWork/dao"
	"RedWork/model"
	"RedWork/util"
	"encoding/json"
	"fmt"
	"github.com/go-sql-driver/mysql"
	"log"
)

func QueryAllGoods(page_g int) (data []model.GoodInfo) {
	fmt.Println("page_v =", page_g)
	goods := make([]model.GoodInfo, 0)
	//var goods []good_info
	var sqlStr string
	if page_g != -1 {
		sqlStr = fmt.Sprintf("select good_id, good_name, price, store_id, store_name, category, good_image, introduce from good_info limit %d,10", page_g)
	} else {
		sqlStr = "select good_id, good_name, price, store_id, store_name, category, good_image, introduce from good_info"
	}

	rows, err := dao.DB.Query(sqlStr)
	if ok, errR, _ := util.ErrHandle(err); ok {
		fmt.Println(errR.(*mysql.MySQLError).Message)
	} else {
		fmt.Println(err)
	}
	fmt.Println("rows=", rows)
	defer rows.Close()
	for rows.Next() {
		var good_data model.GoodInfo
		err := rows.Scan(
			&good_data.GoodID,
			&good_data.GoodName,
			&good_data.Price,
			&good_data.StoreID,
			&good_data.StoreName,
			&good_data.Category,
			&good_data.Good_image,
			&good_data.Introduce)
		fmt.Println(good_data)
		if err != nil {
			if ok, err, _ := util.ErrHandle(err); ok {
				fmt.Println(err)
			}
			fmt.Println(err)
			break
		}
		goods = append(goods, good_data)
		//fmt.Println(goods)
	}
	if len(goods) == 0 {
		return nil
	}
	fmt.Println(goods[0])
	fmt.Println(goods[1])
	bytes, _ := json.Marshal(goods)
	fmt.Println(string(bytes))
	return goods
}

func QueryAKindGood(goodname string, sort int) (data []model.GoodInfo) {
	fmt.Println("goodname = ", goodname)
	//goods := make([]model.GoodInfo, 0)
	//sqlStr := fmt.Sprintf("select good_id, good_name, price, store_id, store_name, category, good_image,introduce from good_info where good_name like '%%%s%%'", goodname)
	db := gdb.Table("good_info").Select("good_id, good_name, price, "+
		"store_id, store_name, category, good_image,introduce").Where("good_name like ?", "%"+goodname+"%").Group("price")
	if sort == 1 {
		//sqlStr = sqlStr + " order by turnover desc"
		if err := db.Order("turnover desc").Find(&data); err != nil {
			log.Println(err)
			return []model.GoodInfo{}
		}
	} else if sort == 4 {
		//sqlStr = sqlStr + " order by price asc"
		if err := db.Order("price asc").Find(&data).Error; err != nil {
			log.Println(err)
			return []model.GoodInfo{}
		}
	} else if sort == 5 {
		//sqlStr = sqlStr + " order by price desc"
		if err := db.Order("price desc").Find(&data).Error; err != nil {
			log.Println(err)
			return []model.GoodInfo{}
		}
	}

	bytes, _ := json.Marshal(data)
	fmt.Println(string(bytes))
	return data
}

func QueryCategoryGoods(code int) (data []model.GoodInfo) {
	//goods := make([]model.GoodInfo, 0)
	fmt.Println("QueryCategoryGood = ", code)
	//sqlStr := fmt.Sprintf("select good_id, good_name, price, store_id, store_name, category, good_image,introduce from good_info where category = %d", code)
	err := gdb.Table("good_info").Select("good_id, good_name, price, store_id, "+
		"store_name, category, good_image,introduce").Where("category = ?", code).Find(&data)
	if err != nil {
		log.Println(err)
		return
	}
	if len(data) == 0 {
		return nil
	}
	bytes, _ := json.Marshal(data)
	fmt.Println(string(bytes))
	return data
}
